<?php
/**
 * Department Controller
 * This controller handles the insertion, edit and removal of departments from the application.
 * @author ovo
 *
 */
class Admin_DeptController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function addDeptAction(){
    	
    	$this->view->title = "Add a Department to the list of Departments";
    	$form = new Admin_Form_addDept();
    	$this->view->form = $form;
    	
    	if ($this->getRequest()->isPost()){
    	$formData = $this->getRequest()->getPost();
    
    		if ($form->isValid($formData)){
    			
    			//$schoolid = $form->getValue('school');
    			$deptName = $form->getValue('dept');
    			$enterDept = new Admin_Model_DbTable_Department();
    			$enterDept->addDept($deptName);
    			$this->view->message = "$deptName has been added to the database";
    		}
    	}
    }

    /**
     * editDept:
     * This function edits the department of a particular university
     */
    public function editdeptAction(){
    
        // action body
        $this->view->title = "Edit a Department";
        $form = new Admin_Form_addDept();
        $form->submit->setLabel('Edit');
        $this->view->form = $form;
        
		 if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				if ($form->isValid($formData)) {
				$id = (int)$form->getValue('id');
				$name = $form->getValue('dept');
				$dept = new Admin_Model_DbTable_Department();
				$dept->updateDept($id, $name);
				$this->_redirect('/admin/dept/viewdepts');
		} else {
				$form->populate($formData);
		}
		} else {
		$id = $this->getRequest()->getParam('id');
		if ($id > 0) {
		$dept = new Admin_Model_DbTable_Department();
		$form->populate($dept->getDept($id));
		}
}
    	
    }
    
    /**
     * deletedept: This function deletes the department of a particular university/
     */
    public function deletedeptAction() {
    	$id = $this->getRequest()->getParam('id');
        $dept = new Admin_Model_DbTable_Department();
    	$dept->deleteDept($id);
    	$this->_redirect('/admin/dept/viewdepts');
    }
    
    /**
     * 
     */
    public function viewdeptsAction() {
    	
    	$dept = new Admin_Model_DbTable_Department();
        $this->view->depts = $dept->viewDepts();
    }
}

